﻿using Microsoft.AspNetCore.Mvc;
using SkiaSharp;
using System.Diagnostics;
using System.Web;
using WebMvcDemo.Models;

namespace WebMvcDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        /// <summary>
        /// 创建Skia图片
        /// </summary>
        /// <param name="id">前端html设置img的src的id参数变化，可以更新图片</param>
        /// <returns></returns>
        public IActionResult GetImg(string id)
        {
            _logger.LogInformation($"{DateTime.Now}, 开始创建图片");

            //创建画布的容器
            var info = new SKImageInfo(600, 300);
            using var surface = SKSurface.Create(info);
            var canvas = surface.Canvas;

            canvas.Clear(SKColors.SkyBlue);

            using var paint = new SKPaint
            {
                Color = SKColors.Black,
                IsAntialias = true,
                Typeface = SkiaChinaFont.ChinaFont,
                TextSize = 24
            };

            string msg = $"{DateTimeOffset.Now:T}, 还有1万行Skia绘图代码...";
            canvas.DrawText(msg, 0, 30, paint);

            using var linePaint = new SKPaint()
            {
                Color = (DateTimeOffset.Now.Second % 4 <= 1) ? SKColors.Red : SKColors.Green,
                Style = SKPaintStyle.Stroke,//不填充
                StrokeWidth = 3,
            };
            canvas.DrawRect(10, 50, info.Width - 20, info.Height - 60, linePaint);

            msg += $", linePaint.Color={linePaint.Color}, skContainer.CanvasSize={info.Size}";
            Debug.WriteLine(msg);

            //把画布容器转换为图片文件
            using var image = surface.Snapshot();
            using var data = image.Encode(SKEncodedImageFormat.Png, 100);

            return File(data.ToArray(), "image/png");

        }

        /// <summary>
        /// 导出Skia绘图PDF文件
        /// </summary>
        /// <returns></returns>
        public IActionResult ExportSkiaPdf()
        {
            //导出PDF文件
            var skiaPdfMaker = new SkiaPdfMaker();

            var ary = skiaPdfMaker.ExportToPdf();

            string httpHeader = $"attachment; filename={DateTime.Now:yyyyMMdd-HHmmss}.pdf";
            HttpContext.Response.Headers.Add("content-disposition", httpHeader);

            return File(ary, "application/pdf");
        }

        /// <summary>
        /// 导出iText绘图PDF文件
        /// </summary>
        /// <returns></returns>
        public IActionResult ExportItextPdf()
        {
            //导出PDF文件
            var itextPdfMaker = new ItextPdfMaker();

            var ary = itextPdfMaker.ExportToPdf();

            string httpHeader = $"attachment; filename={DateTime.Now:yyyyMMdd-HHmmss}-itext.pdf";
            HttpContext.Response.Headers.Add("content-disposition", httpHeader);

            return File(ary, "application/pdf");
        }
    }
}